//
//  RightDatePicker.m
//  Common
//
//  Created by guoshiqing on 2018/4/18.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "RightDatePicker.h"
#import "Macro.h"
#import "UIView+DDAddition.h"

@interface RightDatePicker ()
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UILabel *lblStart;
@property (nonatomic, strong) UILabel *lblEnd;

@property (nonatomic, copy) NSString *strStartDate;
@property (nonatomic, copy) NSString *strEndDate;
@end


@implementation RightDatePicker
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        [self addSubviews];
    }
    return self;
}


- (void)addSubviews
{
    [self addSubview:self.lblStart];
    [self addSubview:self.lblEnd];
    [self addSubview:self.dpkStart];
    [self addSubview:self.dpkEnd];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.dpkStart.width = self.width - self.lblStart.right;
    self.dpkStart.left = self.lblStart.right;
    self.dpkEnd.width = self.width - self.lblEnd.right;
    self.dpkEnd.left = self.dpkStart.left;
    self.dpkEnd.top = self.height/2;
}

#pragma mark - 懒加载

- (UILabel *)lblStart
{
    if (!_lblStart) {
        _lblStart = [[UILabel alloc] initWithFrame:CGRectMake(Cell_X, 0, 60.0f, 20.0f)];
        _lblStart.centerY = self.height/4;
        _lblStart.font = [UIFont systemFontOfSize:13];
        _lblStart.textAlignment = NSTextAlignmentCenter;
        _lblStart.textColor = [EPColor epColor_Blue];
        _lblStart.text = @"起始时间";
    }
    return _lblStart;
}

- (UILabel *)lblEnd
{
    if (!_lblEnd) {
        _lblEnd = [[UILabel alloc] initWithFrame:CGRectMake(Cell_X, 0, 60.0f, 20.0f)];
        _lblEnd.centerY = self.height - self.height/4;
        _lblEnd.font = [UIFont systemFontOfSize:13];
        _lblEnd.textAlignment = NSTextAlignmentCenter;
        _lblEnd.textColor = [EPColor epColor_Blue];
        _lblEnd.text = @"结束时间";
    }
    return _lblEnd;
}

- (UIDatePicker *)dpkStart
{
    if (!_dpkStart) {
        _dpkStart = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height/2)];
        _dpkStart.datePickerMode = UIDatePickerModeTime;
        [_dpkStart setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"NL"]];
        [_dpkStart addTarget:self action:@selector(pickerValueChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _dpkStart;
}

- (UIDatePicker *)dpkEnd
{
    if (!_dpkEnd) {
        _dpkEnd = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height/2)];
        _dpkEnd.datePickerMode = UIDatePickerModeTime;
        [_dpkEnd setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"NL"]];
        [_dpkEnd addTarget:self action:@selector(pickerValueChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _dpkEnd;
}


#pragma mark - private action
// 选择完成后
- (void)pickerValueChange:(UIDatePicker *)datePicker  {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *currentDateString = [dateFormatter stringFromDate:datePicker.date];
    if (datePicker == self.dpkStart) {
        self.strStartDate = currentDateString;
    }else if(datePicker == self.dpkEnd) {
        self.strEndDate = currentDateString;
    }
    
    // 返回修改后的时间
    if (self.timerBlock) {
        self.timerBlock(self.strStartDate, self.strEndDate);
    }
}


-(void)dealloc {
    NSLog(@"dealloc");
}


// 初始化时间为当前时间
- (void)setTimerStr:(NSString *)timerStr
{
    _timerStr = timerStr;
    if (_timerStr) {
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"HH:mm"];
        
        self.strStartDate = timerStr;
        self.strEndDate = timerStr;
        self.dpkStart.date = [dateFormat dateFromString:timerStr];
        self.dpkEnd.date = [dateFormat dateFromString:timerStr];
    }
}

@end
